app.controller('ContainerCtrl', ['$scope', '$location', 'AuthService', function($scope, $location, AuthService){

	$scope.init = function() {
		AuthService.getCurrentUser().then(function(resp) {
			$scope.user = resp.data;
		});
	};
	
	$scope.init();
	
	$scope.$on('login', function(event, user){
		$scope.user = user;
	});
	
	$scope.$on('logout', function(){
		$scope.user = undefined;
	});
	
	$scope.logout = function () {
		AuthService.logout()
	    .then(function (response) {
	    	$scope.$emit('logout');
	        $location.url("/login");
	    }, function (error) {
	        console.log(error);
	    });
	};
	
}]);

app.controller('LoginCtrl', ['$scope', '$location', 'AuthService', function($scope, $location, AuthService){

	$scope.login = function() {
		AuthService.login({'username': $scope.uname, 'password': $scope.pwd, 'remember': $scope.remember})
		.then(function(resp) {
			$scope.$emit('login', resp.data);
	        $location.url("/home");
		}, function(error) {
			console.log(error);
			$scope.error = error.data.message;
		});
	};
	
}]);

app.controller('HomeCtrl', ['$scope', '$location', function($scope, $location){
	
}]);

app.controller('ChangePasswordCntrl', ['$scope', '$location', 'UserService', function($scope, $location, UserService){
	$scope.change = function() {
		if($scope.pwd != $scope.cnfpwd) {
			alert("Confirm password did not match");
			return;
		}
		UserService.changePassword($scope.pwd)
		.then(function(resp) {
			$location.path('');
		}, function(error) {
    		$scope.error = error.data;
		});
	};
}]);

app.controller('ContactCtrl', ['$scope', '$location', 'ContactService', function($scope, $location, ContactService){
	
	$scope.viewType = "grid";
	$scope.contacts = [];
    $scope.totalContacts = 0;
    $scope.contactsPerPage = 10;
    $scope.pagination = {
        current: 1
    };
    
    function getResultsPage(pageNumber) {
        var result = ContactService.getAll(pageNumber);
        //TODO: do after get all promise is resolved
        $scope.totalContacts = result.count;
        $scope.contacts = result.contacts;
    }
    
    getResultsPage(1);
    
    $scope.deleteContact = function(contact) {
		ContactService.deleteContact(contact);
		//TODO: do after delete promise is resolved
		getResultsPage($scope.pagination.current);
	};
	
	$scope.changeView = function(value) {
		$scope.viewType = value;
	};
	
	$scope.pageChanged = function(newPage) {
        getResultsPage(newPage);
    };
	
	$scope.gotoContactPage = function(contact){
		ContactService.set(contact);
		$location.path('contactEntry');
	};
	
}]);

app.controller('ContactEntryCtrl', ['$scope', '$location', 'ContactService', function($scope, $location, ContactService){
	
	$scope.contact = ContactService.get();
	
	$scope.save = function() {
		console.log($scope.contact);
		ContactService.save($scope.contact);
		//TODO: do after save promise is resolved
		$location.path('contacts');
	};
	
}]);

app.controller('UserCtrl', ['$scope', '$location', 'UserService', function($scope, $location, UserService){
	
	$scope.users = [];
    $scope.totalUsers = 0;
    $scope.usersPerPage = 10;
    $scope.pagination = {
        current: 1
    };
    
    function getResultsPage(pageNumber) {
        UserService.getAll(pageNumber, $scope.usersPerPage).then(function(resp) {
        	$scope.totalUsers = resp.data.count;
            $scope.users = resp.data.data;
        }, function(error) {
    		$scope.error = error.data;
        });
    }
    
    getResultsPage(1);
    
    $scope.enableUser = function(user) {
    	UserService.enable(user.username)
    	.then(function(resp){
    		user.enabled = true;
    	}, function(error) {
    		$scope.error = error.data;
    	});
	};
	
	$scope.disableUser = function(user) {
    	UserService.disable(user.username)
    	.then(function(resp){
    		user.enabled = false;
    	}, function(error) {
    		$scope.error = error.data;
    	});
	};
	
	$scope.pageChanged = function(newPage) {
        getResultsPage(newPage);
    };
	
	$scope.gotoUserPage = function(user){
		UserService.set(user);
		$location.path('userEntry');
	};
	
}]);

app.controller('UserEntryCtrl', ['$scope', '$location', 'UserService', function($scope, $location, UserService){
	
	$scope.user = UserService.get();
	$scope.edit = false;
	if($scope.user) {
		$scope.edit = true;
		$scope.user.password = "";
		$scope.user.admin = $scope.user.roles.indexOf("Admin") != -1;
	} else {
		$scope.user = {};
	}
	
	$scope.save = function() {
		if(angular.isUndefined($scope.user.password)) {
			alert("password cannot be empty");
			return;
		}
		if($scope.user.password != $scope.user.confirmpassword) {
			alert("Confirm password did not match");
			return;
		}
		if($scope.edit) {
			UserService.update($scope.user)
			.then(function(resp) {
				gotoUsers();
			}, function(error) {
	    		$scope.error = error.data;
			});
		} else {
			UserService.save($scope.user)
			.then(function(resp) {
				gotoUsers();
			}, function(error) {
	    		$scope.error = error.data;
			});
		}
	};
	
	gotoUsers = function() {
		UserService.set(undefined);
		$location.path('users');
	};
	
}]);
